<?php

namespace App\Http\Controllers;

use App\Http\Resources\SignResource;
use App\Models\Sign;
use App\Http\Requests\StoreSignRequest;
use App\Http\Requests\UpdateSignRequest;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class SignController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth:sanctum'])->except(['index']);
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return AnonymousResourceCollection
     */
    public function index(Request $request): AnonymousResourceCollection
    {
        $sings = Sign::whereDate('created_at', now())->paginate($request->query('row', 10));
        return SignResource::collection($sings);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param StoreSignRequest $request
     * @param Sign $sign
     * @return array
     * @throws AuthorizationException
     */
    public function store(StoreSignRequest $request, Sign $sign): array
    {
        $this->authorize('create', Sign::class);
        $sign->user_id = Auth::id();
        $sign->fill($request->input())->save();
//        Auth::user()->signs()->create($request->input());
        return $this->success('签到成功', $sign);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Sign $sign
     * @return array
     * @throws AuthorizationException
     */
    public function destroy(Sign $sign): array
    {
        $this->authorize('delete', Sign::class);
        $sign->delete();
        return $this->success('删除成功');
        //
    }
}
